package com.robot.backend.utils.gameMap;

import com.robot.backend.consumer.WebSocketServer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.robot.backend.utils.gameMap.cell;

import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class player {//player记录各自用户的信息，例如step执行的操作序列，cells即当前蛇的长度
    private int id;
    private int sx;//开始的坐标
    private int sy;
    private List<Integer> step;//移动的步骤
    private List<cell> cells;//蛇的位置
    private int botid;
    public int hx;
    public int hy;
    public int tx;
    public int ty;

    private boolean tailLonger(int n){//用回合数检查是否变长
        if (n <= 10) return true;
        if (n%3 == 1) return true;//不变长
        return false;//变长
    }
    public List<cell> getCells(){
        List<cell> cells1 = new ArrayList<>();
        int[] dx={-1,0,1,0},dy={0,1,0,-1};
        int x = sx,y=sy;//当前蛇头
        int stepn = 0;//回合数
        cells1.add(new cell(x,y));//加入新的蛇头
        for(int d :step){
            x+=dx[d];
            y+=dy[d];
            cells1.add(new cell(x,y));
            //WebSocketServer.users.get(id).game.updateMap(x,y,0);
            if (!tailLonger( ++ stepn)) {
                tx=cells1.get(0).x;
                ty=cells1.get(0).y;
                WebSocketServer.users.get(id).game.g[tx][ty] = 0;
                cells1.remove(0);//取掉蛇头，即不变长
                //WebSocketServer.users.get(id).game.updateMap(x,y,1);
            }
        }
        return cells1;
    }

    public void getHead(){
        hx = this.sx;
        hy = this.sy;
        List<cell> cells1 = new ArrayList<>();
        int[] dx={-1,0,1,0},dy={0,1,0,-1};
        int x = this.sx,y=this.sy;//当前蛇头
        int stepn = 0;//回合数
        cells1.add(new cell(x,y));//加入新的蛇头
        for(int d :step){
            x+=dx[d];
            y+=dy[d];
            cells1.add(new cell(x,y));
        }
        hx = cells1.get(cells1.size()-1).x;
        hy = cells1.get(cells1.size()-1).y;
    }
    public void updateMap(){
        WebSocketServer.users.get(id).game.g[hx][hy] = 1;

//        WebSocketServer.users.get(id).game.updateMap(hx,hy,0,this.id);
//           if (!tailLonger( step.size()+1)) {
//                WebSocketServer.users.get(id).game.updateMap(hx,hy,1,this.id);
//            }
//        int[] dx={-1,0,1,0},dy={0,1,0,-1};
//        int x = sx,y=sy;//当前蛇头
//        int stepn = 0;//回合数
//        for(int d :step){
//            x+=dx[d];
//            y+=dy[d];
//            if(x <=0 || y<= 0 ) return;
//            WebSocketServer.users.get(id).game.updateMap(x,y,0,this.id);
//            if (!tailLonger( ++ stepn)) {
//                WebSocketServer.users.get(id).game.updateMap(x,y,1,this.id);
//            }
//        }
    }
}
